package com.sangeng.ss.controller;

import com.sangeng.ss.domain.DogEvent;
import com.sangeng.ss.service.Animal;
import com.sangeng.ss.service.TestEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：xxx
 * @description：TODO
 * @date ：2024/02/22 11:35
 */
@RestController
public class TestController {

    @Autowired
    List<Animal> animals;

    @Autowired
    Map<String,Animal> animalMap;

    @Autowired
    private TestEvent testEvent;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @PostMapping("/define/hello")
    @PreAuthorize("@SDAuth.hasPermission('hello')")
    public String hello(){
        System.out.println(animalMap);
        return animals.stream().map(Animal::eat).collect(Collectors.joining(","));
    }

    @PostMapping("/define/test")
    @PreAuthorize("@SDAuth.hasPermission('test')")
    public String hello1(){
        return animals.stream().map(Animal::eat).collect(Collectors.joining(","));
    }

    @PostMapping("/origin/bbb")
    @PreAuthorize("hasAuthority('bbb')")
    public String hello2(){
        return animals.stream().map(Animal::eat).collect(Collectors.joining(","));
    }

    @PostMapping("/origin/aaa")
    @PreAuthorize("hasAuthority('aaa')")
    public String hello3(){
        return animals.stream().map(Animal::eat).collect(Collectors.joining(","));
    }

    @GetMapping("none/testEvent")
    public String testEvent(){
        applicationEventPublisher.publishEvent(new DogEvent("大黄狗"));
        return "success";
    }

}
